﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Deeper.DataAccess;
using System.IO;
using Deeper.Common;
using Deeper.CodeBuilder;

namespace CodeBuilder
{
	public partial class CodeBuilder : Form
	{
		public CodeBuilder()
		{
			InitializeComponent();
		}

		private HDataBaseInfo _database;
		private IList<FileInfo> fileList;

		private void btnLoad_Click(object sender, EventArgs e)
		{
			DialogResult result = openFile.ShowDialog();
			if (result == DialogResult.OK)
			{
				try
				{
					_database = BuilderCode.LoadFromPDMXMLDocument(openFile.FileName);
					lstTables.ValueMember = "TableName";
					lstTables.DisplayMember = "Remake";
					lstTables.DataSource = _database.Tables;

				}
				catch (Exception)
				{
					MessageBox.Show("文件打开失败");
				}

			}
			else
			{
				MessageBox.Show("请选择一个文件");
			}
		}


		private void codeBuilder_Load(object sender, EventArgs e)
		{
			IniHelper ini = new IniHelper("config.ini");

			txtModelNamespace.Text = ini.IniReadValue("System", "model", "Model");
			txtProjectNamespace.Text = ini.IniReadValue("System", "project", "Hydra");
			txtServiceNamespace.Text = ini.IniReadValue("System", "Service", "Service");
			txtPath.Text = ini.IniReadValue("System", "path");
			txtAuthorName.Text = ini.IniReadValue("System", "Author");
			DirectoryInfo dires = new DirectoryInfo(Application.StartupPath);
			ddlTemplate.Items.Clear();
			ddlTemplate.DataSource = dires.GetDirectories();
			ddlTemplate.DisplayMember = "Name";
			ddlTemplate.ValueMember = "Name";

		}

		private void codeBuilder_FormClosing(object sender, FormClosingEventArgs e)
		{
			IniHelper ini = new IniHelper("config.ini");
			ini.IniWriteValue("System", "project", txtProjectNamespace.Text);
			ini.IniWriteValue("System", "model", txtModelNamespace.Text);
			ini.IniWriteValue("System", "Service", txtServiceNamespace.Text);
			ini.IniWriteValue("System", "path", txtPath.Text);
			ini.IniWriteValue("System", "Author", txtAuthorName.Text);

		}

		private void btnChoise_Click(object sender, EventArgs e)
		{
			DialogResult result = folderBrowserDialog1.ShowDialog();
			if (result == DialogResult.OK)
			{
				txtPath.Text = folderBrowserDialog1.SelectedPath;
			}
			else
			{
				MessageBox.Show("请选择一个目录");
			}
		}



		private void btnExport_Click(object sender, EventArgs e)
		{
			try
			{
				for (int i = 0; i < lstTables.Items.Count; i++) ExportFile(i);

				MessageBox.Show("导出成功！");
			}
			catch
			{
				MessageBox.Show("导出失败！");
			}
		}

		private void GetCode(int index)
		{
			HTableAttribute table = _database.Tables[index];
			fileList.ToList().ForEach(file =>
			{
				TextBox txt = tabMain.TabPages[file.Name].Controls[0] as TextBox;
				if (!txt.IsNull())
				{
					txt.Text = builder.BuilderModel(table, txtProjectNamespace.Text, txtModelNamespace.Text, txtServiceNamespace.Text, txtAuthorName.Text, file.FullName);
				}
			});
		}



		/// <summary>
		/// 导出文件
		/// </summary>
		/// <param name="i">表索引</param>
		private void ExportFile(int index)
		{
			HTableAttribute table = _database.Tables[index];
			try
			{
				fileList.ToList().ForEach(file =>
				{
					string txt = builder.BuilderModel(table, txtProjectNamespace.Text, txtModelNamespace.Text, txtServiceNamespace.Text, txtAuthorName.Text, file.FullName);
					string path = string.Format("{0}\\{1}", txtPath.Text, file.Name.Substring(0, file.Name.IndexOf(".")));
					string name = string.Format("{0}\\{1}", path, file.Name.Replace("Entity", table.EntityName));
					FileHelper.WriteTextFile(name, txt, Encoding.UTF8, false, true);
				});
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
		}

		private void lstTables_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (lstTables.SelectedIndex >= 0)
			{
				GetCode(lstTables.SelectedIndex);
			}

		}

		private void ddlTemplate_SelectedIndexChanged(object sender, EventArgs e)
		{
			DirectoryInfo dires = new DirectoryInfo(Application.StartupPath + "\\" + ddlTemplate.SelectedValue);
			tabMain.TabPages.Clear();
			fileList = dires.GetFiles().ToList();
			fileList.ToList().ForEach(file =>
			{
				tabMain.TabPages.Add(file.Name, file.Name);
				TextBox txt = new TextBox();
				txt.Multiline = true;
				txt.Top = 5;
				txt.Left = 5;
				txt.ScrollBars = ScrollBars.Both;
				txt.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Right;
				txt.Height = tabMain.TabPages[file.Name].Height - 10;
				txt.Width = tabMain.TabPages[file.Name].Width - 10;
				tabMain.TabPages[file.Name].Controls.Add(txt);
			});
		}

	}
}
